package org.example.Controller;

import org.example.Exception.EmpExistException;
import org.example.Pojo.Depth;
import org.example.Pojo.Result;
import org.example.Server.DepthServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/depts")
public class DepthController {

    @Autowired
    private DepthServer depthServer;

    /*
    * 响应查询功能
    * */
    @GetMapping
    public Result list(){
        List<Depth> list=depthServer.findAll();
        return Result.success(list);
    }

    /*
    * 删除数据通过Id
    * */
    @DeleteMapping
    public Result deleteById(@RequestParam("id") Integer id) throws EmpExistException {
        depthServer.deleteById(id);
        return Result.success();
    }

    /*
     * 添加部门名字
     * */
    @PostMapping
    public Result insert(@RequestBody Depth depth){
        depthServer.insert(depth);
        return Result.success();
    }


    /*
     * 查找通过Id
     * */
    @PutMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id){
        System.out.println("查询的id:"+id);
        Depth depth=depthServer.findById(id);
        return Result.success(depth);
    }

    /*
     * 修改部门通过ID
     * */
    @PutMapping
    public Result updateById(@RequestBody Depth depth){
        System.out.println(depth);
        depthServer.updateById(depth);
        return Result.success();
    }
}
